package org.example.controller;

import org.example.entity.CommonResult;
import org.example.entity.Follower;
import org.example.entity.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.example.service.FollowerService;
import org.example.service.UserService;

import javax.annotation.Resource;
import java.util.List;

@RequestMapping("/follower")
@RestController
public class FollowerController {

    @Resource
    FollowerService followerService;

    @Resource
    UserService userService;

    /**
     * 功能：添加关注
     * @param follower
     * @return 响应信息
     */
    @RequestMapping("/follow")
    public CommonResult<Follower> follow(@RequestBody Follower follower){
        if(followerService.addFollower(follower)){
            User user = userService.getUser(follower.getFollowerId());
            user.setFans(user.getFans()+1);
            userService.updateUser(user);
            return new CommonResult<>(200,"success",null);
        }
        return new CommonResult<>(400,"error",null);
    }

    /**
     * 功能：取消关注
     * @param follower
     * @return 响应信息
     */
    @RequestMapping("/withdraw")
    public CommonResult<Follower> withdrawFollow(@RequestBody Follower follower){
        if(followerService.deleteFollower(follower)){
            User user = userService.getUser(follower.getFollowerId());
            user.setFans(user.getFans()-1);
            userService.updateUser(user);
            return new CommonResult<>(200,"success",null);
        }
        return new CommonResult<>(400,"error",null);
    }

    /**
     * 功能：通过fansId得到你的关注
     * @param fansId
     * @return 关注
     */
    @RequestMapping("/getFollowers")
    public CommonResult<List<Follower>> getFollowers(@RequestParam("fansId") Long fansId){
        if(followerService.getFollowers(fansId)!=null){
            return new CommonResult<>(200,"success",followerService.getFollowers(fansId));
        }
        return new CommonResult<>(400,"error",null);
    }

    /**
     * 功能：通过fansId得到你的粉丝
     * @param followerId
     * @return 粉丝
     */
    @RequestMapping("/getFans")
    public CommonResult<List<Follower>> getFans(@RequestParam("followerId") Long followerId){
        if(followerService.getFans(followerId)!=null){
            return new CommonResult<>(200,"success", followerService.getFans(followerId));
        }
        return new CommonResult<>(400,"error",null);
    }
}
